package com.xsir.controller;

import com.qiniu.util.Auth;
import com.xsir.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author user
 * @date 2020/9/12
 */
@RestController
@Slf4j
public class APIController {
    @Value("${qny.accessKey}")
    private String accessKey;

    @Value("${qny.secretKey}")
    private String secretKey;

    @Value("${qny.bucket}")
    private String bucket;
    
    /**
     * 获取上传凭证
     */
    @GetMapping("/qny/token")
    public Map<String, Object> getUploadToken() {
        Map<String, Object> map = new HashMap<>(4);
        String token = Auth.create(accessKey, secretKey).uploadToken(bucket);
        Integer code;
        String msg;
        
        if(token.isEmpty()){
            code = 500;
            msg = "获取token失败";
        } else {
            code = 200;
            msg = "OK";
        }
        
        map.put("code", code);
        map.put("msg", msg);
        map.put("token", token);
        
        return map;
    }
}
